﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace WMH_Project
{
    public partial class MainForm : Form
    {
        AlgorithmController algorithmController;
        private AlgorithmStatistics algorithmStatistics;
        public MainForm(AlgorithmController ac)
        {
            algorithmController = ac;
            InitializeComponent();
            this.FormClosing += new System.Windows.Forms.FormClosingEventHandler(this.MainForm_FormClosing);
            CreateHandle();
            openFileDialog.FileOk += loadFile;
            openFileDialog.Filter = "Graph file|*.graph";
            saveGraphDialog.Filter = "Graph file|*.graph";
            saveStatisticsDialog.Filter = "Statistics file|*.stat";
            algorithmController.newBest = new AlgorithmController.NewBest(newBest);
            algorithmController.executed = new AlgorithmController.Executed(executed);
            algorithmController.nextIteration = new AlgorithmController.NextIteration(nextIteration);
        }

        private void nextIteration(IterationStat itStat)
        {
            itertionNumberText.Text = itStat.IterationNum.ToString();
            averageText.Text = itStat.AverageQuality.ToString();
            itSinceLastBestText.Text = itStat.ItSinceLastBest.ToString();
            algorithmStatistics.Stats.AddLast(itStat);
        }

        private void newBest(Specimen specimen)
        {
            Logger.WriteLine("Nowy najlepszy:" + specimen.ToString());
            textBox3.Text = specimen.GetQuality().ToString();
        }
        private void executed(string typ, STOP_REASON stopReason)
        {
            if (typ == "finish")
            {
                
                button1.Enabled = true;
                button2.Enabled = false;
                button2.Text = "Stop";
                //if(algorithmController.)
                if (stopReason == STOP_REASON.NoNewBestSpeciment)
                {
                    Logger.WriteLine("osiagnieto koniec: Nie udało się znaleźć nowego osobnika");
                    noNewBestRadio.Checked = true;
                    maxIterRadio.Checked = false;
                }
                if (stopReason == STOP_REASON.MaxIterations)
                {
                    Logger.WriteLine("osiagnieto koniec: Wykonano wszystkie zaplanowane iteracje");
                    noNewBestRadio.Checked = false;
                    maxIterRadio.Checked = true;
                }
                zapiszStatystykiToolStripMenuItem.Enabled = true;
            }
            else if (typ == "start")
            {
                Logger.WriteLine("rozpoczeto");
                button1.Enabled = false;
                button2.Enabled = true;
                zapiszStatystykiToolStripMenuItem.Enabled = false;
                button2.Text = "Stop";
            }
            else
            {
                Logger.WriteLine("przerwano");
                button1.Enabled = true;
                button2.Enabled = true;
                button2.Text = "Continue";
            }
        }
        private void WMH_Load(object sender, EventArgs e)
        {

        }

        private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            Environment.Exit(0);
        }
        private void zapiszWynikiToolStripMenuItem_Click(object sender, EventArgs e)
        {
            saveStatisticsDialog.ShowDialog();
            StreamWriter sw = File.CreateText(saveStatisticsDialog.FileName);
            sw.WriteLine("Jenny mam na imię Jenny");
            sw.Close();
         
        }

        private void wczytajGrafToolStripMenuItem_Click(object sender, EventArgs e)
        {
            openFileDialog.ShowDialog();
        }

        public void loadFile(object sender, CancelEventArgs e)
        {
             if(openFileDialog.FileName == null) return;
             try
             {
                 //FileStream fileStr = new FileStream(openFileDialog.FileName, FileMode.Open);
                 using (StreamReader sr = new StreamReader(openFileDialog.FileName))
                 {
                     Graph graph = new Graph(sr);
                     Logger.WriteLine("graf zostal wczytany");
                     setGraph(graph);
                 }
             }
             catch (IOException ex)
             {
                 Logger.WriteLine("Nie udało się wczytać pliku z grafem:\n"+ex);
             }
        }
        void setGraph(Graph graph)
        {
            algorithmController.SpecimenFactory = new SpecimenFactory(graph);
            button1.Enabled = true;
            button4.Enabled = true;
            numericUpDown6.Value = graph.NumberOfX;
            numericUpDown7.Value = graph.NumberOfY;
        }

        private void zamknijToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void button1_Click_2(object sender, EventArgs e)
        {
            MUTATION_TYPE mt = MUTATION_TYPE.Normal;
            if (weightedRadio.Checked)
                mt = MUTATION_TYPE.Weighted;

            algorithmStatistics = new AlgorithmStatistics();
           
            MutationParam mp = new MutationParam((double)numericUpDown3.Value, mt);
            StopCondition sc = new StopCondition((int)numericUpDown4.Value,(int)numericUpDown5.Value);           
            algorithmController.Start((int)numericUpDown1.Value, (int)numericUpDown2.Value, sc, mp);
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (button2.Text == "Stop")
            {
                algorithmController.Stop();
            }
            else
            {
                algorithmController.Continue();
            }
        }

        private void zapiszStatystykiToolStripMenuItem_Click(object sender, EventArgs e)
        {
            saveStatisticsDialog.ShowDialog();
        }

        private void button4_Click(object sender, EventArgs e)
        {
            saveGraphDialog.ShowDialog();
        }

        private void saveStatisticsDialog_FileOk(object sender, CancelEventArgs e)
        {
            if (saveStatisticsDialog.FileName == null) return;
            StreamWriter sw = new StreamWriter(saveStatisticsDialog.FileName);
            sw.Write(algorithmStatistics.ToString());
            sw.Close();
            Logger.WriteLine("zapisano Statystyki");
        }

        private void saveGraphDialog_FileOk(object sender, CancelEventArgs e)
        {
            if (saveGraphDialog.FileName == null) return;
            StreamWriter sw = new StreamWriter(saveGraphDialog.FileName);
            sw.Write(algorithmController.SpecimenFactory.Graph.ToString());
            sw.Close();
            Logger.WriteLine("zapisano Graf");
        }

        private void button3_Click(object sender, EventArgs e)
        {
            Logger.WriteLine("rozpoczeto generowanie");
            Graph g = new Graph((int)numericUpDown6.Value, (int)numericUpDown7.Value, (int) numericMaxCost.Value);
            setGraph(g);
        }

        private void normalRadio_CheckedChanged(object sender, EventArgs e)
        {
            if (normalRadio.Checked == true)
                weightedRadio.Checked = false;
        }

        private void weightedRadio_CheckedChanged(object sender, EventArgs e)
        {
            if (weightedRadio.Checked == true)
                normalRadio.Checked = false;
        }

        private void numericMaxCost_ValueChanged(object sender, EventArgs e)
        {

        }


    }
}
